#include "pch.h"
#include "globalupdatetask.h"
#include "util.h"
#include "crpc.h"
#include "util/dbutil.h"
#include "utildb/db_interface.h"

CGlobalUpdateTask::CGlobalUpdateTask()
:CMysqlTask("GlobalUpdateTask")
,m_dwRpcDelayed(0)
{
}

CGlobalUpdateTask::~CGlobalUpdateTask()
{ 
}

void CGlobalUpdateTask::Execute(IMysqlConn* poMysqlConn, char* pcBuffer, INT32 nLength)
{
	//LogDebug("Before CGlobalUpdateTask::Execute0 [%p]", this);
	SetMysqlError(MYSQL_ERR_FAILED);
	//LogDebug("Before CGlobalUpdateTask::Execute [%p]", this);
	
	//insert role
	std::stringstream ss;
	//char buffer[128] = {0};
	//std::string sqlstr_;
	//std::string param;
	//ss << m_uID;
	//ss >> param;
	//LogDebug("Before CGlobalUpdateTask::Execute*** [%p]", &ss);
	ss << "insert into globalconfig(_id, data) values(" << m_uID;
	ss << ", '";
	//snprintf(buffer, sizeof(buffer),"%d", m_uID);
	//param = buffer;
	//sqlstr_ = "insert into globalconfig(_id, data) values(" + param;

	LogDebug("Before CGlobalUpdateTask::Execute2 [%p]", this);
	if(-1 == poMysqlConn->EscapeString(m_strData.c_str(), (INT32)m_strData.length(), pcBuffer, nLength)) return;
	
	//snprintf(buffer, sizeof(buffer), "insert into globalconfig(_id, data) values(%d, '%s') on duplicate key update data='%s'", m_uID, pcBuffer, pcBuffer);
	ss << pcBuffer << "') on duplicate key update data='" << pcBuffer << "'";
// 	sqlstr_ += "\"";
// 	sqlstr_ += pcBuffer;
// 	sqlstr_ += "\"";
// 	sqlstr_ += ") on duplicate key update data=";
// 	sqlstr_ += "\"";
// 	sqlstr_ += pcBuffer;
// 	sqlstr_ += "\"";


	LogDebug("Before CGlobalUpdateTask::Execute3 [%p]", this);
	if(!poMysqlConn->Query(ss.str().c_str(), ss.str().size() /*sqlstr_.c_str(), sqlstr_.size()*/, NULL))
	{
		GetErrStream() << "ErrDesc: " << poMysqlConn->GetError() << ", ErrNo: " << poMysqlConn->GetErrno();
		return;
	}
	LogDebug("Before CGlobalUpdateTask::Execute4 [%p]", this);
	SetMysqlError(MYSQL_ERR_SUCESS);
	//LogDebug("Before CGlobalUpdateTask::Execute5 [%p]", this);
}

void CGlobalUpdateTask::OnReply()
{
	KKSG::ErrorCode nErrCode = KKSG::ERR_SUCCESS;
	if(m_nMysqlErr != MYSQL_ERR_SUCESS)
	{
		nErrCode = KKSG::ERR_FAILED;
		LogError("update global update fail, id: %d, errMsg: %s", m_uID, GetErrStream().str().c_str());
	}
}


CGlobalUpdateTask2::CGlobalUpdateTask2():DBTaskBase(),m_dwRpcDelayed(0)
{

}

CGlobalUpdateTask2::~CGlobalUpdateTask2()
{

}

bool CGlobalUpdateTask2::db_thread_process()
{
	SetMysqlError(MYSQL_ERR_FAILED);
	std::stringstream ss;
	ss << "insert into globalconfig(_id, data) values(" << m_uID;
	ss << ", '";

	//LogDebug("Before CGlobalUpdateTask::Execute2 [%p]", this);
	if (-1 == pdbi_->EscapeString(m_strData.c_str(), (INT32)m_strData.length(), NULL, 0)) return false;

	char* pcBuffer = pdbi_->GetEscapStringBuff();
	//snprintf(buffer, sizeof(buffer), "insert into globalconfig(_id, data) values(%d, '%s') on duplicate key update data='%s'", m_uID, pcBuffer, pcBuffer);
	ss << pcBuffer << "') on duplicate key update data='" << pcBuffer << "'";


	//LogDebug("Before CGlobalUpdateTask::Execute3 [%p]", this);
	if (!pdbi_->query(ss.str().c_str(), ss.str().size() /*sqlstr_.c_str(), sqlstr_.size()*/,false, NULL))
	{
		GetErrStream() << "ErrDesc: " << pdbi_->getstrerror() << ", ErrNo: " << pdbi_->getlasterror();
		return false;
	}
	//LogDebug("Before CGlobalUpdateTask::Execute4 [%p]", this);
	SetMysqlError(MYSQL_ERR_SUCESS);
	return false;
}

thread::TPTask::TPTaskState CGlobalUpdateTask2::presentMainThread()
{
	KKSG::ErrorCode nErrCode = KKSG::ERR_SUCCESS;
	if (m_nMysqlErr != MYSQL_ERR_SUCESS)
	{
		nErrCode = KKSG::ERR_FAILED;
		LogError("update global update fail, id: %d, errMsg: %s", m_uID, GetErrStream().str().c_str());
	}

	return DBTaskBase::presentMainThread()/*thread::TPTask::TPTASK_STATE_COMPLETED*/;
}